/*
 * @Author: 'zhangkeqq' '2211709169@qq.com'
 * @Date: 2025-07-24 10:14:43
 * @LastEditors: 'zhangkeqq' '2211709169@qq.com'
 * @LastEditTime: 2025-07-31 15:24:22
 * @FilePath: \node-express\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//导入express
const express = require('express')
//导入cors 
const cors = require('cors')
//导入 bodyParser
const bodyParser = require('body-parser')
const {expressjwt:jwt} = require('express-jwt')
const jwtconfig = require('./jwt-config/index')
const Joi = require('joi')
//创建express实例
const app = express()
// Multer 是一个 node.js 中间件，用于处理 multipart/form-data 类型的表单数据，它主要用于上传文件。
const multer = require('multer')
//在server服务器端新建一个public文件，在public文件下新建upload文件用于存放图片
const upload = multer({
    dest:'./public/upload'
})
app.use(upload.any())
//静态托管
app.use(express.static("./public"))



app.use((req,res,next)=>{
    //status =0为成功，=1为失败，默认设为1 ,方便处理失败的情况
    res.cc = (err,status=1)=>{
        res.send({
            status,
            //判断error是错误对象还是字符串
            message:err instanceof Error ? err.message : err
        })
    }
    next()
})
//全局挂载
app.use(cors())
// parse application/x-www-form-urlencoded
//当extended为false时，值为数组或者字符串，当为true时，值可以为任意类型
app.use(bodyParser.urlencoded({extended:false}))
// parse application/json
app.use(bodyParser.json())

// app.use(jwt({
// 	secret:jwtconfig.jwtSecretKey,
//     algorithms:['HS256']
// }).unless({
// 	path:[/^\/api\//]
// }))

const loginRouter = require('./router/login')
app.use('/api',loginRouter)
const userRouter = require('./router/userinfo')
app.use('/user',userRouter)
const setRouter = require('./router/setting')
app.use('/set',setRouter)
const productRouter = require('./router/product.js')
app.use('/pro', productRouter)
const messageRouter = require('./router/message.js')
app.use('/msg', messageRouter)
const fileRouter = require('./router/file.js')
app.use('/file', fileRouter)
const loginLogRouter = require('./router/login_log.js')
app.use('/llog', loginLogRouter)
const operationLogRouter = require('./router/operation_log.js')
app.use('/olog', operationLogRouter)
const overviewLogRouter = require('./router/overview.js')
app.use('/ov', overviewLogRouter)
const depMsgRouter = require('./router/department_msg.js')
app.use('/dm', depMsgRouter)

//对不符合joi规则的情况进行报错
app.use((err,req,res,next) => {
    if(err instanceof Joi.ValidationError){
        res.send({
            status:1,
            message:'输入的数据不符合验证规则'
        })
    }
})


//监听端口
app.listen(9000,()=>{
    console.log('监听9000端口')
})